﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLMemberPointMS.Common;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Entity;

using DLMemberPointMS.Business;
using DLMemberPointMS.Entity.ViewModel;
using System.IO;

namespace DLMemberPointMS.Web.Controllers
{
    public class PresentController : Controller
    {
        private ExchangGiftsService _exchangGiftsService = new ExchangGiftsService();
        // GET: Present
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public ActionResult PreBind()
        {
            return View();
        }
        [HttpPost]
        public ActionResult PreBind(PrePage view)
        {
            var res = _exchangGiftsService.GetEGS(view);
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <returns></returns>
        public ActionResult PreInsert()
        {
            return View();
        }
        [HttpPost]
        public ActionResult PreInsert(PreInsertView model)
        {
            var res = _exchangGiftsService.InsertMemCards(model);
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult PreUpdate(int Id)
        {
            var model = _exchangGiftsService.GetUpdateEGS(Id).data;
            var viewmod = (PreInsertView)model;
            ViewBag.img = viewmod.EG_Photo;
            return View("PreInsert", model);
        }
        [HttpPost]
        public ActionResult PreUpdate(PreInsertView view)
        {
            var model = _exchangGiftsService.UpdateEGC(view);
            return Json(model);
        }
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PreDele(int EG_ID)
        {
            var model = _exchangGiftsService.DeleEXC(EG_ID);
            return Json(model);
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public ActionResult UploadImgRes(HttpPostedFileBase file)
        {
            //获取存储的路径
            string path = Server.MapPath(string.Format("~/{0}/", "UploadImg"));
            //截取图片后缀名
            string fileName = file.FileName.Substring(file.FileName.LastIndexOf("."));
            //转换时间
            string time = DateTime.Now.ToString("yyyy-MM-dd");
            //拼接文件名，时间+4位随机数+原文件名
            fileName = time + "-" + new Random().Next(1001, 9999) + fileName;
            //保存文件
            string a = Path.Combine("/UploadImg", fileName);
            string filePath = Path.Combine(path, fileName);
            file.SaveAs(filePath);
            //得到文件的保存地址 并将文件保存地址放入到数据库中(没写)
            //var res = _memCardsService.UploadImgSaveDb(path);
            return Json(new OperateResult(code.Success, "上传成功",a));
        }
    }
}
